<?php

class ActionHelper_Abstract_Process extends ActionHelper_Abstract_Extend  {	
	
	protected function _pagelistProcess(){
		$counts = $this->_pageInfo->getCounts ();
		if (empty ( $counts )) {
			$counts = $this->_moduleSelect->getCounts ( $this->_whereArray );
			$this->_pageInfo->setCounts ( $counts );
		}
		$data = null;
		$html = null;
		if (!empty ( $counts )) {
			if (! $this->_isPagelist) {
				$data = $this->_moduleSelect->getData ( $this->_whereArray );
			} else {
				$this->_pageInfo->addInfo ( $this->_pageUrl, $this->_pagenumber, $this->_linklist );				
				$data = $this->_moduleSelect->getDataByPageInfo ( $this->_whereArray, $this->_pageInfo );
				$html = $this->_moduleSelect->getFormatHtmlByPageInfo ( $this->_pageInfo );
			}			
		}	
		if (! empty ( $data )) {
			$this->view->data = $data;
			$this->view->html = $html;
		} else {
			$this->view->imgageType = Htmlhelper_Img::IMG_WARNING;
			$this->view->nodata = $this->_translator->_ ( $this->_i18nMsg [self::NO_DATA] );
		}	
	}
	
	protected function _searchProcess(){
		if ($this->getRequest ()->isPost ()) {
			$this->getActionController ()->getHelper ( 'viewRenderer' )->setNoRender ();
			$this->_formData = $this->getActionController ()->getPosts ();
			if ($this->_form->isValid ( $this->_formData )) {
				$_SESSION [$this->_session_name] = $this->_formData;				
				$whereArray = $this->_moduleSelect->getWhereArray ( $this->_formData );
				$this->_whereArray = array_merge ( $whereArray, $this->_whereArray );
				$this->_pagelistProcess ( );
			} else {
				$this->view->imgageType = Htmlhelper_Img::IMG_FAIL;
				$this->view->message = $this->_translator->_ ( $this->_i18nMsg [self::INPUT] );
				$this->view->errors = $this->_form->translateError ();
			}
		} else {
			if (isset ( $_SESSION [$this->_session_name] ) and $this->_pageInfo->isPagelist ()) {
				$this->_formData = $_SESSION [$this->_session_name];			
				$whereArray = $this->_moduleSelect->getWhereArray ( $this->_formData );
				$this->_whereArray = array_merge ( $whereArray, $this->_whereArray );
				$this->_pagelistProcess ( );
			}else{
				if(isset ( $_SESSION [$this->_session_name] )){
					unset ( $_SESSION [$this->_session_name] );
				}
			}
		}
	}
	
	protected function _doProcessBefore() {
		return true;
	}
	protected function _postProcess(){
		 if ($this->getRequest ()->isPost ()) {	
			//edit or new	
			//请求更新message而已	
			$this->getActionController ()->getHelper ( 'viewRenderer' )->setNoRender ();
			$this->_formData = $this->getActionController ()->getPosts ();
			if ($this->_form->isValid ( $this->_formData )) {
				$options = $this->_changeI18nTranslate ();
				//
				$check = $this->_doProcessBefore ();
				if (! $check) {
					return;
				}
				//
				if ($this->isEditProcess ()) {
					$_oprationFunction = $this->_oprationUpdateFunction;
					$this->_moduleOpration->setCheckBeforeUpdate ( $this->_isCheckBeforeUpdate );
					$check = $this->_moduleOpration->$_oprationFunction ( $this->_oprationField, $this->_id, $this->_formData );
				} else if ($this->isRegisterProcess ()) {
					$_oprationFunction = $this->_oprationInsertFunction;
					$check = $this->_moduleOpration->$_oprationFunction ( $this->_formData );
				}				
				if (empty ( $check )) {
					$this->view->imgageType = Htmlhelper_Img::IMG_FAIL;
					$this->view->errors = $this->_translator->translateError ( $options );
					if ($this->isEditProcess ()) {
						$this->view->message = $this->_translator->_ ( $this->_i18nMsg [self::FAIL_UPDATE], $options );
					}elseif ($this->isRegisterProcess ()) {
						$this->view->message = $this->_translator->_ ( $this->_i18nMsg [self::FAIL_REGISTER], $options );
					}
				} else {
					$this->view->imgageType = Htmlhelper_Img::IMG_SUCCESS;
					if ($this->isEditProcess ()) {
						$this->view->message = $this->_translator->_ ( $this->_i18nMsg [self::SUCCESS_UPDATE], $options );
					}elseif ($this->isRegisterProcess ()) {
						$this->view->message = $this->_translator->_ ( $this->_i18nMsg [self::SUCCESS_REGISTER], $options );
					}
				}
			} else {
				$this->view->imgageType = Htmlhelper_Img::IMG_FAIL;
				$this->view->message = $this->_translator->_ ( $this->_i18nMsg [self::INPUT] );
				$this->view->errors = $this->_form->translateError ();
			}
		}			
	}
	protected function _orderDownOrUpProcess(){
		if ($this->isOrderUpProcess () or $this->isOrderDownProcess()) {
			if($this->_moduleOrderOpration instanceof Module_Interface_Orderlist ){
				if($this->isOrderDownProcess()){
					$check = $this->_moduleOrderListOpration->orderlistDown ( $this->_id );	
				}else{
					$check = $this->_moduleOrderListOpration->orderlistUp ( $this->_id );
				}	
				if (! $check) {
					$this->view->imgageType = Htmlhelper_Img::IMG_FAIL;
					$this->view->message = $this->_translator->_ ( $this->_i18nMsg [self::FAIL_ORDER] );
					$options = $this->_changeI18nTranslate ();
					$this->view->errors = $this->_translator->translateError ( $options );
				}else{
					$this->view->imgageType = Htmlhelper_Img::IMG_SUCCESS;
					$this->view->message = $this->_translator->_ ( $this->_i18nMsg [self::SUCCESS_ORDER] );
				}			
			}else{
				throw new Exception('firstly set orderlist module');
			}			
		}		
	}	
	protected function _deleteProcess(){
		if ($this->isDeleteProcess ()) {			
			if($this->_updateToNoShow){				
				$check = $this->_moduleOpration->falseDeleteByField($this->_oprationField,$this->_id,$this->_updateToNoShowArray );
			}else{
				$check = $this->_moduleOpration->deleteByField ( $this->_oprationField, $this->_id );
			}
			if (! $check) {
				$this->view->imgageType = Htmlhelper_Img::IMG_FAIL;
				$this->view->message = $this->_translator->_ ( $this->_i18nMsg [self::FAIL_DELETE] );
				$options = $this->_changeI18nTranslate ();
				$this->view->errors = $this->_translator->translateError ( $options );
			}else{
				$this->view->imgageType = Htmlhelper_Img::IMG_SUCCESS;
				$this->view->message = $this->_translator->_ ( $this->_i18nMsg [self::SUCCESS_DELETE] );
			}
		}
	}
	
	protected function _initDefaultObject() {
		if (!empty($this->_id)) {
			if (! empty ( $this->_initObject )) {
				$result = $this->_initObject;
			} else {
				$result = $this->_moduleSelect->getByField ( $this->_selectField, $this->_id );
				if (empty ( $result )) {
					$this->getActionController ()->getHelper ( 'viewRenderer' )->setNoRender ();
					$this->view->message = $this->_translator->_ ( $this->_i18nMsg [self::NO_EXIST] );
					$this->view->imgageType = Htmlhelper_Img::IMG_WRONG;					
					return false;
				}
			}
			$name = $this->_objectnameInView;
			$this->view->$name = $result;
			$data = $result->getInfo ();
			$this->_defaultData = $data;
			if ($this->_isPopulateFormData and ! empty ( $this->_form )) {
				$this->_form->populate ( $this->_defaultData );
			}
		}
		return true;
	}

}